package com.blog.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class StrUtil {
    public static String getOnlyFileId(String fileName){
        String extension = fileName.substring(fileName.lastIndexOf('.'));
        String OnlyFileId = System.currentTimeMillis() + UUID.randomUUID().toString() + extension;
        return OnlyFileId;
    }
    public static Timestamp getTimestamp(){
        Date date = new Date();
        return new Timestamp(date.getTime());
    }

    //注意：（在数据交互的过程中）转换给前端的时间格式要和接受的时间转换格式一直，不然会抛出异常
    public static String formattedTime(Timestamp timestamp){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        return sdf.format(timestamp);
    }
    //注意：转换给前端的时间格式要和接受的时间转换格式一直，不然会抛出异常
    public static Timestamp getFormattedTimestamp(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        Date DateTimestamp = new Date();
        try {
            DateTimestamp = sdf.parse(time);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Timestamp(DateTimestamp.getTime());
    }
}
